package com.runner.boost;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.runner.entity.CollisionEntity;
import com.runner.observer.Event;
import com.runner.observer.Observer;
import com.runner.observer.Subject;
import com.runner.player.Player;
import com.runner.track.Block;
import com.runner.utility.Constants;
import com.runner.utility.Locator;

public class ShieldBoost extends CollisionEntity implements Boost, Constants, Observer {

	private Player player;
	public static final float S_SCALE = 0.18F;
	private Subject subject;
	private float destroyTimer;
	private ParticleEffect particles;
	private boolean destroy;

	public ShieldBoost(Player p, Subject subject) {
		super(new Vector2(p.getPosition()), TYPE.SHIELD, SHIELD_PATH, false, true, false, S_SCALE);
		player = p;
		this.subject = subject;
		destroy = false;
		subject.addObserver(this);
		destroyTimer = 0;
		player.addShield();
	}

	@Override
	public void activate() {
	}

	@Override
	public void update(float delta) {
		super.update(delta);
		if (destroy) {
			if (destroyTimer < 1) {
				physics.getBody().setLinearVelocity(-Block.SPEED * Locator.getXGameSpeed() * delta, 0);

				Vector3 parPos = new Vector3(this.getPhysics().getBody().getPosition().x, this.getPhysics().getBody().getPosition().y, 0);
				Locator.getCamera().project(parPos);
				particles.getEmitters().get(0).setPosition(parPos.x, parPos.y);

			} else {
				exists = false;
				destroy = false;
			}
			destroyTimer += delta;
		}
	}

	@Override
	public void render(SpriteBatch sb) {
		if (!destroy) {
			this.getPhysics().getBody().setTransform(player.getPhysics().getBody().getPosition(), 0);
			super.render(sb);
		}
	}

	@Override
	public void collide(CollisionEntity otherBody) {
		if (!destroy && otherBody.getPhysics().getType().equals(TYPE.BLOCK)) {

		}

	}

	@Override
	public void onNotify(Event event) {
		switch (event) {
		case EVENT_INVULN:
			player.removeShield();
			break;
		case HIT_WITH_SHIELD:
			//if we havent been hit yet
			if (!destroy) {
				Locator.scheduleEvent(0.4f, Event.EVENT_INVULN, subject);
				hit();
			}
			break;
		default:
			break;
		}

	}

	public void hit() {
		destroy = true;
		// particles
		particles = new ParticleEffect();
		particles.load(Gdx.files.internal("data/art/block_explode"), Gdx.files.internal("data/art/"));
		Locator.getEffects().add(particles);
		Vector3 parPos = new Vector3(this.getPhysics().getBody().getPosition().x, this.getPhysics().getBody().getPosition().y, 0);
		Locator.getCamera().project(parPos);
		particles.setPosition(parPos.x, parPos.y);
		particles.start();
	}

	@Override
	public void destroy() {
		delete();
	}

}
